import request from '@/utils/request'
import { AxiosResponse } from 'axios'

export interface ResAdd{
  id?: number
  name: string
  categoryId: number
  url: string
  description?: string
}

export interface CateAdd{
  id?: number
  name?: string
  sort?: number
}

interface ResPage{
  id?: number
  name?: string
  startCreateTime?: string
  url?: string
  categoryId?: number|null
  endCreateTime?: string
  current?: number
  size?: number
}

// 保存或者更新资源
export const saveOrUpdate = (data:ResAdd):Promise<AxiosResponse> => {
  return request({
    method: 'POST',
    url: '/boss/resource/saveOrUpdate',
    data
  })
}

// 获取所有资源
export const getAllResource = ():Promise<AxiosResponse> => {
  return request({
    method: 'GET',
    url: '/boss/resource/getAll'
  })
}

// 查询资源分类列表
export const getAllCategory = ():Promise<AxiosResponse> => {
  return request({
    method: 'GET',
    url: '/boss/resource/category/getAll'
  })
}

// 保存或更新资源分类
export const saveOrderUpdate = (data:CateAdd):Promise<AxiosResponse> => {
  return request({
    method: 'POST',
    url: '/boss/resource/category/saveOrderUpdate',
    data
  })
}

// 删除资源分类
export const deleteCate = (id:number):Promise<AxiosResponse> => {
  return request({
    method: 'DELETE',
    url: `/boss/resource/category/${id}`
  })
}

// 删除资源
export const deleteRes = (id:number):Promise<AxiosResponse> => {
  return request({
    method: 'DELETE',
    url: `/boss/resource/${id}`
  })
}

// 按条件分页查询资源
export const getResourcePages = (data:ResPage):Promise<AxiosResponse> => {
  return request({
    method: 'POST',
    url: '/boss/resource/getResourcePages',
    data
  })
}

export const getRoleResources = (roleId:number|string):Promise<AxiosResponse> => {
  return request({
    method: 'GET',
    url: '/boss/resource/getRoleResources',
    params: {
      roleId
    }
  })
}

export const allocateRoleResources = (data:any):Promise<AxiosResponse> => {
  return request({
    method: 'POST',
    url: '/boss/resource/allocateRoleResources',
    data
  })
}
